package com.ucap.weking.guangan.dialog;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.support.design.widget.TextInputLayout;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.ucap.weking.guangan.R;
import com.ucap.weking.guangan.base.BaseDialog;
import com.ucap.weking.guangan.tools.PrefUtils;
import com.ucap.weking.guangan.views.MyToast;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by weking on 2017/11/16.
 */

public class PersonlDialog extends AlertDialog {

    @InjectView(R.id.et_name)
    TextInputLayout tlName;
    @InjectView(R.id.et_phone)
    TextInputLayout tlPhone;
    @InjectView(R.id.btn_save)
    Button btnSave;

    public PersonlDialog(Context context) {
        super(context);
        setData();
    }

    protected PersonlDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        setData();
    }

    protected PersonlDialog(Context context, int themeResId) {
        super(context, themeResId);
        setData();

    }

    public void setData() {
        View contentView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_personl, null);
        setView(contentView);
        ButterKnife.inject(this, contentView);
        String personl_name = PrefUtils.getString("personl_name", "", getContext());
        String personl_phone = PrefUtils.getString("personl_phone", "", getContext());
        tlName.getEditText().setText(personl_name);
        tlPhone.getEditText().setText(personl_phone);
        save();
    }

    private void save() {
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = tlName.getEditText().getText().toString();
                String phone = tlPhone.getEditText().getText().toString();
                if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(phone)) {
                    tlName.setErrorEnabled(false);
                    if (phone.length() == 11) {
                        tlPhone.setErrorEnabled(false);
                        PrefUtils.putString("personl_name", name, getContext());
                        PrefUtils.putString("personl_phone", phone, getContext());
                        dismiss();
                        MyToast.showTrue(getContext(), "已保存！");
                    } else {
                        tlPhone.setError("请输入正确的电话");
                    }

                } else if (TextUtils.isEmpty(name) && !TextUtils.isEmpty(phone)) {
                    tlName.setError("请输入姓名");
                    tlPhone.setErrorEnabled(false);
                } else if (!TextUtils.isEmpty(name) && TextUtils.isEmpty(phone)) {
                    tlPhone.setError("请输入电话");
                    tlName.setErrorEnabled(false);
                } else if (TextUtils.isEmpty(name) && TextUtils.isEmpty(phone)) {
                    tlName.setError("请输入姓名");
                    tlPhone.setError("请输入电话");

                }
            }
        });
    }
}
